import axios from 'axios'
import { Toast } from 'vant'

const baseURL = process.env.NODE_ENV === 'development'
  ? 'http://quanzhan.site:3000'
  : 'http://www.xiongmaoyouxuan.com'

const service = axios.create({
  baseURL,
  timeout: 10000,
})

service.interceptors.request.use(config => {
  Toast.loading({
    message: '加载中...',
    duration: 0,
  })
  return config
}, err => {
  Toast.clear()
  return Promise.reject(err)
})

service.interceptors.response.use(response => {
  Toast.clear()
  if (response.status >= 200 && response.status <= 300) {
    const { code, data, msg } = response.data
    if (code === 200) {
      return data
    }
    const error = new Error(msg)
    return Promise.reject(error)
  }
  const err = new Error('接口访问异常')
  return Promise.reject(err)
}, err => {
  Toast.clear()
  return Promise.reject(err)
})

export default service
